﻿Imports Microsoft.Win32
Imports System.Reflection

Partial Public Class GeneralSettingsWindow

    Private ReadOnly Property Context() As GeneralSettings
        Get
            Return DirectCast(Me.DataContext, GeneralSettings)
        End Get
    End Property

    Public Sub New(ByVal settings As GeneralSettings)
        Me.InitializeComponent()

        Me.DataContext = settings


        'Dim x = Me.Owner.Left + (Me.Owner.Width - Me.Width) / 2
        'Dim y = Me.Owner.Top + (Me.Owner.Height - Me.Height) / 2

        'Dim workingArea = Forms.Screen.GetWorkingArea(New System.Drawing.Rectangle(Me.Owner.Left, Me.Owner.Top, Me.Owner.Width, Me.Owner.Height))
        'If workingArea.Bottom < y + messageBoxRect.Height Then
        '    y = workingArea.Bottom - messageBoxRect.Height
        'End If
        'If workingArea.Right < x + messageBoxRect.Width Then
        '    x = workingArea.Right - messageBoxRect.Width
        'End If
        'If y < workingArea.Top Then
        '    y = workingArea.Top
        'End If
        'If x < workingArea.Left Then
        '    x = workingArea.Left
        'End If

    End Sub

    Private Sub SetCurrentVersionRun()
        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", My.Application.Info.ProductName, Assembly.GetEntryAssembly().Location)
    End Sub

    Private Sub DeleteCurrentVersionRun()
        '' TODO 権限が必要
        'Using key As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run")
        '    Try
        '        key.DeleteValue(My.Application.Info.ProductName)
        '    Catch ex As Exception

        '    End Try
        'End Using
    End Sub

#Region "OK Cancel"

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        BindingGroup.CommitEdit()

        If Me.Context.LaunchOnStartup Then
            SetCurrentVersionRun()
        Else
            DeleteCurrentVersionRun()
        End If

        Me.Close()
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub

#End Region


End Class
